var cmenu;
var dhxWins;
function init() {
	/* 初始化日历 */
	initial('ttdiv');
	dhxWins = new dhtmlXWindows();
}

/**
 * 调用创建日程窗口(每次只能调用一个窗口)
 * 
 * @param {}
 *            url
 * @param {}
 *            datestr
 * @param {}
 *            datelabel
 */
function handleNeu(url, datestr, datelabel) {
	if (dhxWins.window("win1")) {
		alert("请先设置当前日程");
	} else {
		createHidden("id", "");
		
		var w1 = dhxWins.createWindow("win1", 50, 50, 600, 400);
		// w1.center();
		w1.denyMove();
		w1.centerOnScreen();
		w1.button("minmax2").hide();
		w1.button("minmax1").hide();
		w1.setText("个人日程-" + datelabel);
		var obj = createNeuCalendar(datestr, datelabel);
		w1.attachObject(obj);
	}
}

/**
 * 处理是否提醒
 */
function handleTX(thizz) {
	if (thizz.checked) {
		$("txarea").style.backgroundColor = "#ffffff";
		$("txdate1").disabled = false;
		$("txtime1").disabled = false;
		$("txmessage1").disabled = false;
		$("edtxtime1").disabled = true;
		$("edtxbtn").style.display = "block";
	} else {
		$("txarea").style.backgroundColor = "#fafafa";
		$("txdate1").disabled = true;
		$("txtime1").disabled = true;
		$("txmessage1").disabled = true;
		$("edtxtime1").disabled = true;
		$("edtxbtn").style.display = "none";
	}
}

/**
 * 提醒方式设置区域的设置
 * 
 * @param {}
 *            vl
 */
function setTX(vl) {
	if (vl == "yes") {
		$("tx1").checked = true;
		$("txarea").style.backgroundColor = "#ffffff";
		$("txdate1").disabled = false;
		$("txtime1").disabled = false;
		$("txmessage1").disabled = false;
		$("edtxbtn").style.display = "block";
	} else {
		$("txarea").style.backgroundColor = "#fafafa";
		$("txdate1").disabled = true;
		$("txtime1").disabled = true;
		$("txmessage1").disabled = true;
	}
}

function setTXType(tp) {

}

/**
 * 编辑个人日程信息
 * 
 * @param {}
 *            id
 * @param {}
 *            datestr
 * @param {}
 *            datelabel
 */
function handleEdit(id, datestr, datelabel) {
	var url = context + "/inner/app/dbtx.do?method=editcalendar&id=" + id;
	var result = sendRequest(url);

	var noresult = xml2parameter(result, "noresult");

	if (noresult == "no") {
		handleNeu("", datestr, datelabel);

		createHidden("id", xml2parameter(result, "ID"));

		// $("dat").value = xml2parameter(result, "CREATETIME");
		$("tlt").value = xml2parameter(result, "TITLE");
		$("cnt").value = xml2parameter(result, "CCONTENT");
		$("sdate1").value = xml2parameter(result, "sdate");
		$("edate1").value = xml2parameter(result, "edate");
		$("stime1").value = xml2parameter(result, "stime");
		$("etime1").value = xml2parameter(result, "etime");
		var icwe = xml2parameter(result, "ICWE");
		var txed = xml2parameter(result, "TXED");

		if (weekendBetween($("sdate1").value, $("edate1").value)) {
			$('icwe').disabled = false;
			$('icwe').checked = (icwe == 1 ? true : false);
		}

		$("txdate1").value = xml2parameter(result, "txdate") == null
				? ""
				: xml2parameter(result, "txdate");
		$("txtime1").value = xml2parameter(result, "txtime") == null
				? ""
				: xml2parameter(result, "txtime");
		if (xml2parameter(result, "txmessage") == "1") {
			$("txmessage1").checked = true;
		}

		var tx = xml2parameter(result, "tx");
		setTX(tx);

		/* 已设置提醒 */
		if ($("tx1").checked) {
			/* 特定日期提示 */
			if ($("sdate1").value == $("edate1").value) {
				$('edtx1').disabled = true;
			} else {
				$('edtx1').disabled = false;
				/* 每天提醒 */
				if (txed == "1") {
					$('edtx1').checked = true;
					selectTxType("everyday");
					$("edtxtime1").value = xml2parameter(result, "txtime");
				} else {

				}
			}

		}

		$("delbutton").style.visibility = "visible";
		/*
		 * $("").value = xml2parameter(result, "noresult"); $("").value =
		 * xml2parameter(result, "noresult"); $("").value =
		 * xml2parameter(result, "noresult"); $("").value =
		 * xml2parameter(result, "noresult"); $("").value =
		 * xml2parameter(result, "noresult"); $("").value =
		 * xml2parameter(result, "noresult"); $("").value =
		 * xml2parameter(result, "noresult");
		 */
	} else {
		alert("没有找到日程信息");
	}
}

/**
 * 创建个人日程
 * 
 * @param {}
 *            datestr 日程时间
 * @param {}
 *            datelabel 日程时间label
 * @return {}
 */
function createNeuCalendar(datestr, datelabel) {
	var str = "<table style=\"width:500px;margin:5px;\">";
	str += "<tr style=\"display:none;\">";
	str += "<td width=\"30%\">&nbsp;<b>日程安排日期</b></td>";
	str += "<td><input type=\"hidden\" class=\"box3\" id=\"dat\" value=\""
			+ datestr + "\">" + datelabel + "</td>";
	str += "</tr>";
	str += "<tr>";
	str += "<td width=\"30%\">&nbsp;<b>日程活动主题</b></td>";
	str += "<td><input type=\"input\" class=\"box3\" id=\"tlt\"></td>";
	str += "</tr>";
	str += "<tr>";
	str += "<td width=\"30%\" valign=\"top\">&nbsp;<b>日程活动内容</b></td>";
	str += "<td><textarea id=\"cnt\" class=\"box3\" style=\"height:50px;\"></textarea></td>";
	str += "</tr>";
	str += "<tr>";
	str += "<td width=\"30%\" valign=\"top\">&nbsp;<b>日程起止时间</b></td>";
	str += "<td><table id=\"datearea\" style=\"border:0px #ccc solid;\" width=\"95%\">";
	str += "<tr>";
	str += "<td>开始日期&nbsp;<input type=\"text\" id=\"sdate1\" class=\"box4\" readonly=\"true\" style=\"width:80px;\" value=\""
			+ datelabel
			+ "\" onclick=\"HS_setDate(this);\">"
			+ "&nbsp;时间&nbsp;<input type=\"text\" id=\"stime1\" class=\"box4\" style=\"width:80px;\" onclick=\"javascript:HS_setTime(this);\" value=\"\">(格式 15:30)"
			+ "</td>";
	str += "</tr>";
	str += "<tr>";
	str += "<td>结束日期&nbsp;<input type=\"text\" id=\"edate1\" class=\"box4\" readonly=\"true\" style=\"width:80px;\" value=\""
			+ datelabel
			+ "\" onclick=\"HS_setDate(this);\">"
			+ "&nbsp;时间&nbsp;<input type=\"text\" id=\"etime1\" class=\"box4\" style=\"width:80px;\" onclick=\"javascript:HS_setTime(this);\" value=\"\">(格式 15:30)"
			+ "</td>";
	str += "</tr>";
	str += "<tr>";
	str += "<td>包含周六、周日&nbsp;<input type=\"checkbox\" id=\"icwe\" value=\"1\" disabled></td>";
	str += "</tr>";
	str += "</table></td>";
	str += "</tr>";
	str += "<tr>";
	str += "<td width=\"30%\" valign=\"top\">&nbsp;<b>日程提醒设置</b><input type=\"checkbox\" id=\"tx1\" onclick=\"javascript:handleTX(this);\">"
			+ "<span id=\"edtxbtn\" style=\"display:none;\">&nbsp;每天提醒&nbsp;<input type=\"checkbox\" id=\"edtx1\" value=\"1\" disabled onclick=\"javascript:this.checked?selectTxType('everyday'):selectTxType('theday');\"></span>"
			+ "</td>";
	str += "<td><table id=\"txarea\" style=\"border:0px #ccc solid;background-color:#fafafa;\" width=\"95%\">";
	str += "<tr id=\"edtxarea\" style=\"display:none;\">";
	str += "<td>时间&nbsp;<input type=\"text\" disabled id=\"edtxtime1\" class=\"box4\" style=\"width:80px;\" onclick=\"javascript:HS_setTime(this);\" value=\"\">(格式 15:30)</td>";
	str += "</tr>";
	str += "<tr id=\"tdtxarea\">";
	str += "<td>日期&nbsp;<input type=\"text\" id=\"txdate1\" class=\"box4\" value=\""
			+ datelabel
			+ "\" disabled readonly=\"true\" style=\"width:80px;\" onclick=\"HS_setDate(this);\">"
			+ "&nbsp;时间&nbsp;<input type=\"text\" disabled id=\"txtime1\" class=\"box4\" style=\"width:80px;\" onclick=\"javascript:HS_setTime(this);\" value=\"\">(格式 15:30)</td>";
	str += "</tr>";
	str += "<tr>";
	str += "<td>短信提醒&nbsp;<input type=\"checkbox\" id=\"txmessage1\" disabled value=\"1\"></td>";
	str += "</tr>";
	str += "</table></td>";
	str += "</tr>";
	str += "<tr>";
	str += "<td colspan=\"2\" align=\"center\"><hr><input type=\"button\" name=\"buttont1\" class=\"button0\" value=\" 提 交 \" onclick=\"javascript:handleSubmit();\">"
			+ "&nbsp;<input type=\"reset\" name=\"buttont1\" class=\"button0\" value=\" 重 置 \">"
			+ "&nbsp;<input type=\"button\" id=\"delbutton\" style=\"visibility:hidden;\" onclick=\"javascript:handleDelete();\" name=\"buttont1\" class=\"button0\" value=\" 删 除 \">"
			+ "</td>";
	str += "</tr>";
	str += "</table>";

	var div = document.createElement("div");
	div.innerHTML = str;

	return div;
}

/**
 * 选择日期后回调函数
 * 
 * @param {}
 *            nm
 */
function catchSelected(obj) {
	if (obj.id == 'edate1' && $('sdate1').value != ''
			&& obj.value > $('sdate1').value
			&& weekendBetween($('sdate1').value, obj.value)) {
		$('icwe').disabled = false;
	} else if (obj.id == 'sdate1' && $('edate1').value != ''
			&& obj.value < $('edate1').value
			&& weekendBetween(obj.value, $('edate1').value)) {
		$('icwe').disabled = false;
	} else {
		$('icwe').disabled = true;
	}

	if ($('sdate1').value != '' && $('edate1').value != ''
			&& ($('sdate1').value == $('edate1').value)) {
		$("edtx1").disabled = true;
		selectTxType('theday');
	} else if ($('sdate1').value != '' && $('edate1').value != ''
			&& ($('sdate1').value < $('edate1').value)) {
		$("edtx1").disabled = false;
		if ($("edtx1").checked) {
			selectTxType('everyday');
		}
	}
}

function selectTxType(tp) {
	if (tp == "everyday") {
		$("edtxarea").style.display = "block";
		$("edtxtime1").disabled = false;
		$("tdtxarea").style.display = "none";
	} else {
		$("edtxarea").style.display = "none";
		$("edtxtime1").disabled = true;
		$("tdtxarea").style.display = "block";
	}
}

/**
 * 显示信息更多列表
 * 
 * @param {}
 *            _date
 * @param {}
 *            _datelabel
 */
function showMoreList(_date, _datelabel) {
	if (dhxWins.window("win2")) {
		// alert("请先设置当前日程");
	} else {
		var url = context + "/inner/app/dbtx.do?method=calendarlist&date="
				+ _date;

		var frm = document.createElement("iframe");
		frm.height = "100%";
		frm.width = "100%";
		frm.frameBorder = "0";
		frm.src = context + "/inner/app/blank.jsp";

		var w2 = dhxWins.createWindow("win2", 50, 50, 750, 600);
		w2.denyMove();
		w2.center();
		w2.button("minmax1").hide();
		w2.setText("日程信息列表-" + _datelabel);
		w2.attachObject(frm);

		frm.src = url;
	}
}

/**
 * 删除日程
 */
function handleDelete() {
	if (confirm("确定删除日程")) {
		var url = context
				+ "/inner/app/dbtx.do?method=deletecalendar&startDate="
				+ startDate + "&endDate=" + endDate;
		document.form1.action = url;
		document.form1.submit();
	}
}

/**
 * 显示日程提醒信息
 * 
 */
function showAlertMessage() {
	var w2 = dhxWins.createWindow("win3", 50, 50, 750, 500);
	win3.center();
	win3.button("minmax1").hide();
	win3.setText("日程提醒");
	win3.attachURL(url);
	w2.stick();
}

/**
 * 提交个人日程信息
 * 
 */
function handleSubmit() {
	if (isblank($("tlt"))) {
		alert("请填写日程活动主题");
	} else if (isblank($("sdate1")) || isblank($("stime1"))) {
		alert("起始时间设置无效");
	} else if (isblank($("edate1")) || isblank($("edate1"))) {
		alert("结束时间设置无效");
	} else if (($("sdate1").value + $("stime1").value) >= ($("edate1").value + $("etime1").value)) {
		alert("起止时间设置无效");
	} else if ($("tx1").checked && !$("edtx1").checked
			&& (isblank($("txdate1")) || isblank($("txtime1")))) {
		/*特定日期提醒*/
		alert("提醒时间设置无效");
	} else if ($("tx1").checked && !$("edtx1").checked
			&& (($("sdate1").value + $("stime1").value) <= ($("txdate1").value + $("txtime1").value))) {
		alert("提醒时间设置无效");
	} else if ($("tx1").checked && $("edtx1").checked
			&& (isblank($("edtxtime1")))) {
		/*每天提醒*/
		alert("提醒时间设置无效");
	} else if (confirm("确定提交")) {
		/*
		 * paramXML("date", $("dat").value); paramXML("title", $("tlt").value);
		 * paramXML("content", $("cnt").value);
		 */
		createHidden("title", $("tlt").value);
		createHidden("createtime", $("dat").value);
		createHidden("remindtime", $("dat").value);
		createHidden("creatorid", userId);
		createHidden("creatorname", userName);
		createHidden("cont", $("cnt").value);
		createHidden("state", "0");
		createHidden("type", "normal");
		createHidden("objid", "0");
		createHidden("ssj", parseTime($("sdate1").value + " "
						+ $("stime1").value + ":00"));
		createHidden("esj", parseTime($("edate1").value + " "
						+ $("etime1").value + ":00"));;

		createHidden("sdate", $("sdate1").value);
		createHidden("stime", $("stime1").value);

		createHidden("edate", $("edate1").value);
		createHidden("etime", $("etime1").value);

		createHidden("icwed",
				($("icwe").disabled == false && $("icwe").checked) ? "1" : "0");

		/*周六,日日程,周六,日显示默认选中*/
		if ($('sdate1').value == $('edate1').value
				&& weekendBetween($('sdate1').value, $('edate1').value)) {
			createHidden("icwed", "1");
		}

		if ($("tx1").checked) {
			createHidden("tx", "yes");
			createHidden("txmessage", $("txmessage1").checked ? $("txmessage1").value : "0");
			createHidden("sms", $("txmessage1").checked ? $("txmessage1").value : "0");
			createHidden("sftx", "1");
			/* 每天提醒 */
			if ($("edtx1").checked) {
				createHidden("txed", "1");
				createHidden("txdate", "");
				createHidden("txtime", $("edtxtime1").value);
				createHidden("txsj", $("edtxtime1").value.replace(":", "")
								+ "00");
			} else {
				createHidden("txed", "0");
				createHidden("txdate", $("txdate1").value);
				createHidden("txtime", $("txtime1").value);
				createHidden("txsj", parseDate($("txdate1").value)
								+ $("txtime1").value.replace(":", "") + "00");
			}
		} else {
			createHidden("tx", "no");
			createHidden("sftx", "0");
			createHidden("txsj", "");
			createHidden("sms", "0");
		}

		var url = context + "/inner/app/dbtx.do?method=savecalendar";
		document.form1.action = url;
		document.form1.submit();
	}
}

function weekendBetween(dtStart, dtEnd) {
	if (typeof dtEnd == 'string')
		dtEnd = StringToDate(dtEnd);
	if (typeof dtStart == 'string')
		dtStart = StringToDate(dtStart);

	var rnt = false;

	var tDate = dtStart;
	while (tDate <= dtEnd) {
		if (tDate.getDay() == 2 || tDate.getDay() == 3) {
			rnt = true;
			break;
		}

		tDate = tDate.valueOf();
		tDate = tDate + 1 * 24 * 60 * 60 * 1000;
		tDate = new Date(tDate);
	}

	return rnt;

}
function StringToDate(DateStr) {
	var converted = Date.parse(DateStr);
	var myDate = new Date(converted);
	if (isNaN(myDate)) {
		var arys = DateStr.split('-');
		myDate = new Date(arys[0], arys[1], arys[2]);
	}
	return myDate;
}
